package site.xuxingcan.xposedxiwang;

import android.os.Environment;

import com.google.gson.Gson;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;


/**
 * Created by xuxingcan on 16-10-24.
 */
public class FileUtil {
    private static volatile FileUtil instance = null;
    private static final File mSettingFile = new File(Environment.getExternalStorageDirectory(), "xw.setting.model");
    private static final String key = "voesujeui2474uyt";
    private Gson gson;
    private SettingModel mSettingModel;

    public static FileUtil getInstance() {
        if (instance == null) {
            synchronized (FileUtil.class) {
                if (instance == null) {
                    instance = new FileUtil();
                }
            }
        }
        return instance;
    }

    private FileUtil() {
        gson = new Gson();
        reloadSetting();
    }

    public boolean writeSetting(SettingModel model) {
        try {
            String s = gson.toJson(model);
            writeFileSdcardFile(mSettingFile, s);
            mSettingModel = model;
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public SettingModel getSettingModel() {
        return mSettingModel;
    }

    public SettingModel reloadSetting() {
        try {
            String s = readFileSdcardFile(mSettingFile);
            mSettingModel = gson.fromJson(s, SettingModel.class);
        } catch (Exception e) {
            e.printStackTrace();
            if (mSettingModel == null) {
                mSettingModel = new SettingModel();
            }
        }
        return mSettingModel;
    }

    //写数据到SD中的文件
    public void writeFileSdcardFile(File path, String write_str) throws Exception {
        FileOutputStream fout = new FileOutputStream(path);
        byte[] bytes = write_str.getBytes();
        fout.write(bytes);
        fout.close();
    }


    //读SD中的文件
    public String readFileSdcardFile(File path) throws Exception {
        String res = "";
        FileInputStream fin = new FileInputStream(path);
        int length = fin.available();
        byte[] buffer = new byte[length];
        fin.read(buffer);
        res = new String(buffer, "UTF-8");
        fin.close();
        return res;
    }
//    private String change(String key) {
//        byte[] bytes = key.getBytes();
//        for (int i = 0; i < bytes.length; i++) {
//            switch (i % 4) {
//                case 0:
//                    bytes[i] = (byte) (bytes[i] >> 2);
//                    break;
//                case 1:
//                    bytes[i] = (byte) (bytes[i] | 0x11);
//                    break;
//                case 2:
//                    bytes[i] = (byte) (bytes[i] & 0x22);
//                    break;
//                case 3:
//                    bytes[i] = (byte) (bytes[i] ^ 0x33);
//                    break;
//            }
//        }
//    }
}
